<?php
###
#  Baing PHP 框架 1.0.0
#  http://baing.co/
#
#  Copyright: 白晓阳
#  License: CC BY-NC-SA 4.0
#  https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh
###

namespace bai\feature;


trait Runnable
{
    protected static array $getters = [];
    protected static array $setters = [];

    protected array $runtime = [];

    abstract public function run();

    public function __set(string $name, array $value)
    {
        if ($name !== 'runtime') return false;

        foreach ($value as $setter => $param) {
            if (!in_array($setter, static::$setters)) continue;
            if (!isset($this->runtime[$setter])) {
                $this->runtime[$setter] = $param;
                continue;
            }
            if (is_array($this->runtime[$setter]) && is_array($param)) {
                $this->runtime[$setter] = $this->runtime[$setter] + $param;
            }
        }
        return true;
    }

    public function __get(string $name)
    {
        if (in_array($name, static::$getters)) {
            return $this->find($name);
        }
        return null;
    }

    final protected function find(string|array $path)
    {
        if (is_string($path) && !str_contains($path, '.')) {
            return $this->runtime[$path] ?? null;
        }
        return Agent::pick($path, $this->runtime);
    }
}